package com.yupi.yupicturebackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yupi.yupicturebackend.model.entity.NFTAsset;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * NFT资产Mapper
 *
 * @author yupi
 */
@Mapper
public interface NFTAssetMapper extends BaseMapper<NFTAsset> {

    /**
     * 根据用户ID查询NFT资产
     *
     * @param userId 用户ID
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE userId = #{userId} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<NFTAsset> selectByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 根据合约地址和代币ID查询NFT
     *
     * @param contractAddress 合约地址
     * @param tokenId 代币ID
     * @return NFT资产
     */
    @Select("SELECT * FROM nft_asset WHERE contractAddress = #{contractAddress} AND tokenId = #{tokenId} AND isDelete = 0")
    NFTAsset selectByContractAndTokenId(@Param("contractAddress") String contractAddress, @Param("tokenId") String tokenId);

    /**
     * 根据创作者查询NFT
     *
     * @param creator 创作者
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE creator = #{creator} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<NFTAsset> selectByCreator(@Param("creator") String creator, @Param("limit") Integer limit);

    /**
     * 根据集合查询NFT
     *
     * @param collection 集合名称
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE collection = #{collection} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<NFTAsset> selectByCollection(@Param("collection") String collection, @Param("limit") Integer limit);

    /**
     * 根据稀有度查询NFT
     *
     * @param rarity 稀有度
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE rarity = #{rarity} AND isDelete = 0 ORDER BY currentPrice DESC LIMIT #{limit}")
    List<NFTAsset> selectByRarity(@Param("rarity") String rarity, @Param("limit") Integer limit);

    /**
     * 查询价格范围内的NFT
     *
     * @param minPrice 最低价格
     * @param maxPrice 最高价格
     * @param currency 货币单位
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE currentPrice BETWEEN #{minPrice} AND #{maxPrice} AND priceCurrency = #{currency} AND tradeable = 1 AND isDelete = 0 ORDER BY currentPrice ASC LIMIT #{limit}")
    List<NFTAsset> selectByPriceRange(@Param("minPrice") BigDecimal minPrice, @Param("maxPrice") BigDecimal maxPrice, @Param("currency") String currency, @Param("limit") Integer limit);

    /**
     * 查询热门NFT（按浏览量排序）
     *
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE isDelete = 0 ORDER BY viewCount DESC LIMIT #{limit}")
    List<NFTAsset> selectPopularNFTs(@Param("limit") Integer limit);

    /**
     * 查询最新铸造的NFT
     *
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE status = 'active' AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<NFTAsset> selectLatestMinted(@Param("limit") Integer limit);

    /**
     * 统计用户NFT数量
     *
     * @param userId 用户ID
     * @return NFT数量
     */
    @Select("SELECT COUNT(*) FROM nft_asset WHERE userId = #{userId} AND isDelete = 0")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 统计各网络NFT数量
     *
     * @return 网络统计
     */
    @Select("SELECT network, COUNT(*) as count FROM nft_asset WHERE isDelete = 0 GROUP BY network")
    List<java.util.Map<String, Object>> countByNetwork();

    /**
     * 统计各状态NFT数量
     *
     * @return 状态统计
     */
    @Select("SELECT status, COUNT(*) as count FROM nft_asset WHERE isDelete = 0 GROUP BY status")
    List<java.util.Map<String, Object>> countByStatus();

    /**
     * 更新NFT浏览量
     *
     * @param id NFT ID
     * @return 更新行数
     */
    @Update("UPDATE nft_asset SET viewCount = viewCount + 1, updateTime = NOW() WHERE id = #{id}")
    int incrementViewCount(@Param("id") Long id);

    /**
     * 更新NFT点赞量
     *
     * @param id NFT ID
     * @return 更新行数
     */
    @Update("UPDATE nft_asset SET likeCount = likeCount + 1, updateTime = NOW() WHERE id = #{id}")
    int incrementLikeCount(@Param("id") Long id);

    /**
     * 更新NFT拥有者
     *
     * @param id NFT ID
     * @param newOwner 新拥有者
     * @param newOwnerAddress 新拥有者地址
     * @return 更新行数
     */
    @Update("UPDATE nft_asset SET currentOwner = #{newOwner}, ownerAddress = #{newOwnerAddress}, updateTime = NOW() WHERE id = #{id}")
    int updateOwner(@Param("id") Long id, @Param("newOwner") String newOwner, @Param("newOwnerAddress") String newOwnerAddress);

    /**
     * 更新NFT价格
     *
     * @param id NFT ID
     * @param price 新价格
     * @param currency 货币单位
     * @return 更新行数
     */
    @Update("UPDATE nft_asset SET currentPrice = #{price}, priceCurrency = #{currency}, updateTime = NOW() WHERE id = #{id}")
    int updatePrice(@Param("id") Long id, @Param("price") BigDecimal price, @Param("currency") String currency);

    /**
     * 搜索NFT（按名称或描述）
     *
     * @param keyword 关键词
     * @param limit 限制数量
     * @return NFT资产列表
     */
    @Select("SELECT * FROM nft_asset WHERE (name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<NFTAsset> searchNFTs(@Param("keyword") String keyword, @Param("limit") Integer limit);
}
